<#if noProjectId??>
<h1>无效的项目信息</h1>
<#elseif notFound>
<h1>未找合同信息信息</h1>
<#else>
<form class="cost-form">
	<input type="hidden" name="id" value="${(data.id)!''}">
	<input type="hidden" name="project.id" value="${(projId)!''}">
	<input type="hidden" name="type" value="${data.type!''}">
	<input type="hidden" name="mainContract" value="${(data.mainContract)?string('true', 'false')}">
	<input type="hidden" name="members[0].company" value="${orgName}">
	<input type="hidden" name="members[0].level" value="LEVEL0">
	<input type="hidden" name="members[1].level" value="LEVEL1">
	<div class="row">
		<div class="col-md-12 col-sm-12 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="text-danger">*</span>
							<span class="title-content">合同名称</span>
						</div>
					</div>
					<input type="text" class="form-control not-null" name="name" placeholder="最多60个中文" value="${data.name}">
				</div>
			</div>
		</div>
		<div class="col-md-6 col-sm-6 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="text-danger">*</span>
							<span class="title-content">合同编号</span>
						</div>
					</div>
					<input type="text" class="form-control not-null" name="no" placeholder="最多30个字符" value="${data.no}">
				</div>
			</div>
		</div>
		<div class="col-md-6 col-sm-6 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="text-danger">*</span>
							<span class="title-content">合同金额</span>
						</div>
					</div>
					<input type="text" class="form-control not-null" name="price" placeholder="请输入数字" value="${data.price}">
				</div>
			</div>
		</div>
		<div class="col-md-6 col-sm-6 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="title-content">结算金额</span>
						</div>
					</div>
					<input type="text" class="form-control" name="payPrice" placeholder="请输入数字" value="${data.payPrice}">
				</div>
			</div>
		</div>
		<div class="col-md-6 col-sm-6 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="title-content">签订日期</span>
						</div>
					</div>
					<input type="text" class="form-control datetimepicker" name="signDate" data-foot="false" data-format="yyyy-MM-dd" data-time="${data.signDate}" placeholder="请选择日期" readonly>
				</div>
			</div>
		</div>
		<hr class="info-split">
		<div class="col-md-6 col-sm-6 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="title-content">起始工期</span>
						</div>
					</div>
					<input type="text" class="form-control datetimepicker" name="start" data-foot="false" data-format="yyyy-MM-dd" data-time="${data.start}" placeholder="请选择日期" readonly>
				</div>
			</div>
		</div>
		<div class="col-md-6 col-sm-6 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="title-content">结束工期</span>
						</div>
					</div>
					<input type="text" class="form-control datetimepicker" name="end" data-foot="false" data-format="yyyy-MM-dd" data-time="${data.end}" placeholder="请选择日期" readonly>
				</div>
			</div>
		</div>
		<hr class="info-split">
		<div class="col-md-12 col-sm-12 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="text-danger">*</span>
							<span class="title-content">签约单位</span>
						</div>
					</div>
					<select class="form-control not-null" name="members[1].company">
						<#list supplierCompany as cc>
						<option value="${cc}" ${(data.customer.company==cc)?string('selected','')}>${cc}</option>
						</#list>
					</select>
				</div>
			</div>
		</div>
		<div class="col-md-6 col-sm-6 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="title-content">联系人</span>
						</div>
					</div>
					<input type="text" class="form-control" name="members[1].people" value="${data.customer.people}" placeholder="最多10个汉字">
				</div>
			</div>
		</div>
		<div class="col-md-6 col-sm-6 col-xs-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div style="width: 88px;">
							<span class="title-content">联系电话</span>
						</div>
					</div>
					<input type="text" class="form-control" name="members[1].phone" value="${data.customer.phone}" placeholder="最多15个数字">
				</div>
			</div>
		</div>
		<div class="col-md-12 col-sm-12 col-xs-12">
			<div class="btn btn-danger pull-right cancel" style="margin-left: 10px;">取消</div>
			<#if !data.id??>
			<div class="btn btn-warning pull-right continue" style="margin-left: 10px;">确定并继续添加</div>
			</#if>
			<div class="btn btn-primary pull-right ok">确定</div>
		</div>
	</div>
</form>
<script type="text/javascript">
	$(function() {
		$.suredy.datetimepicker.init();

		var validate = function($form) {
			var valid = true;
			$('.not-null', $form).each(function(i) {
				var $this = $(this);

				if ($this.val() === '') {
					valid = false;
					return false;
				}
			});

			return valid;
		};

		var edit = function(comeOn) {
			var $form = $('form.cost-form');
			var projId = $('input[name="project.id"]', $form).val();

			if (!validate($form)) {
				alert('带有[*]项为必填项！请认真填写！');
				return false;
			}

			$form.ajaxSubmit({
				url : $.suredy.contextPath + '/contract/form/update-child',
				type : 'post',
				success : function(success, textStatus, jqXHR) {
					if (!success) {
						alert('新建[子合同]失败！');
						return false;
					}

					var listUri = $.suredy.contextPath + '/contract/list/children-contract-list/' + projId;

					$.suredy.loadContent(listUri, '.tab-pane.children-contract-info');

					if (!comeOn) {
						$.suredy.closeModal();
					} else {
						$form.clearForm();
					}
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert('新建[子合同]失败！\n' + errorThrown);
				}
			});
		}

		$('.btn.ok').on('click', function(event) {
			edit(false);
		});

		$('.btn.continue').on('click', function(event) {
			edit(true);
		});

		$('.btn.cancel').on('click', function(event) {
			$.suredy.closeModal();
		});
	});
</script>
</#if>
